<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">

			<el-form-item label="出库单号" prop="orderkey">
				<el-input v-model="dataForm.orderkey" placeholder="出库单号"></el-input>
			</el-form-item>
			<el-form-item label="创建人" prop="adduser">
				<el-input v-model="dataForm.adduser" placeholder="创建人"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="addtime">
				<el-input v-model="dataForm.addtime" placeholder="创建时间"></el-input>
			</el-form-item>
			<el-form-item label="更新人" prop="updatuser">
				<el-input v-model="dataForm.updatuser" placeholder="更新人"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updatetime">
				<el-input v-model="dataForm.updatetime" placeholder="更新时间"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getOrderallocationById,
		putOrderallocation,
		postOrderallocation
	} from "@/network/lswms/orders/Orderallocation"
	import {
		ElMessage
	} from 'element-plus'
	import _ from 'lodash'


	export default {
		name: "orderallocationAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				orderkey: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: ''
			});


			const dataRule = reactive({
				orderkey: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				adduser: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				addtime: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				updatuser: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				updatetime: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}]
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getorderallocationInfo();
					}

				})
			}

			const getorderallocationInfo = () => {
				getOrderallocationById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return !dataForm.value.id ? postOrderallocation(dataForm.value) :
							putOrderallocation(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef

			}
		},


	}
</script>

<style scoped>

</style>
